﻿//======================================================================
//
//        Copyright : Zhengzhou Strawberry Computer Technology Co.,LTD.
//        All rights reserved
//        
//        Application:NFinal MVC framework
//        Filename : FilterHelper.cs
//        Description :过滤器帮助类,用于执行设置的过滤器
//
//        created by Lucas at  2015-5-31
//     
//        WebSite:http://www.nfinal.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFinal.Http;

namespace NFinal.Filter
{
    /// <summary>
    /// 过滤器帮助类,用于执行设置的过滤器
    /// </summary>
    public class FilterHelper
    {
        /// <summary>
        /// 参数过滤器
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static bool ParamaterFilter<TContext>(IParameterFilter[] filters,TContext context, NameValueCollection parameters)
        {
            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    if (!filter.ParameterFilter(context,parameters))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 验证过滤器
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <typeparam name="TRequest"></typeparam>
        /// <param name="filters"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static bool ConnectionFilter<TContext>(IConnectionFilter[] filters,TContext context,Http.HttpConnection httpConnection)
        {
            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    if (!filter.ConnectionFilter(context, httpConnection))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <typeparam name="TRequest"></typeparam>
        /// <param name="filters"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static bool BeforeActionFilter<TContext, TRequest>(IBeforeActionFilter[] filters, NFinal.Action.AbstractAction<TContext, TRequest> action)
        {
            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    if (!filter.ActionFilter(action))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        public static bool AfterActionFilter<TContext, TRequest>(IAfterActionFilter[] filters, NFinal.Action.AbstractAction<TContext, TRequest> action)
        {
            if (filters?.Length > 0)
            {
                //倒序执行响应后过滤器
                for (int i = filters.Length-1; i > -1; i--)
                {
                    if (!filters[i].ActionFilter(action))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
